package com.seeTools.next.controller.sys;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seeTools.next.api.entity.sys.SysDictData;
import com.seeTools.next.common.core.PageQuery;
import com.seeTools.next.common.core.Result;
import com.seeTools.next.service.sys.SysDictDataService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: seeTools
 * @Date: 2023/11/1  16:04
 * @Version 1.0
 */
@Validated
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController {

    @Resource
    private SysDictDataService sysDictDataService;


    /**
     * 查询字典数据列表
     *
     * @param pageQuery
     * @return
     */
    @SaCheckPermission("dict.list")
    @PostMapping("/list")
    public Result<Page<SysDictData>> list(@RequestBody PageQuery<SysDictData> pageQuery) {
        Page<SysDictData> list = sysDictDataService.findList(pageQuery);
        return Result.success(list);
    }

    /**
     * 根据字典类型查询字典数据信息
     *
     * @param dictType
     * @return
     */
    @GetMapping(value = "/type/{dictType}")
    public Result<List<SysDictData>> dictType(@PathVariable String dictType) {
        List<SysDictData> data = sysDictDataService.selectDictDataByType(dictType);
        if (ObjectUtil.isNull(data)) {
            data = new ArrayList<SysDictData>();
        }
        return Result.success(data);
    }

    /**
     * 查询字典数据详细
     *
     * @param dictCode
     * @return
     */
    @SaCheckPermission("dict.query")
    @GetMapping(value = "/{dictCode}")
    public Result<SysDictData> getInfo(@PathVariable Long dictCode) {
        return Result.success(sysDictDataService.getInfo(dictCode));
    }

    /**
     * 新增字典类型
     *
     * @param dict
     * @return
     */
    @SaCheckPermission("dict.add")
//    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Void> add(@Validated @RequestBody SysDictData dict) {
        sysDictDataService.save(dict);
        return Result.success();
    }

    /**
     * 修改保存字典类型
     *
     * @param dict
     * @return
     */
    @SaCheckPermission("dict.edit")
//    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Void> edit(@Validated @RequestBody SysDictData dict) {
        sysDictDataService.updateOnLock(dict);
        return Result.success();
    }

    /**
     * 删除字段
     *
     * @param dictCodes
     * @return
     */
    @SaCheckPermission("dict.remove")
//    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/")
    public Result<Void> remove(@RequestBody SysDictData sysDictData) {
        return Result.state(sysDictDataService.removeById(sysDictData));
    }

    /**
     * 导出字典数据列表
     *
     * @param response
     * @param sysDictData
     */
//    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @SaCheckPermission("dict.export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData sysDictData) {
        List<SysDictData> list = sysDictDataService.selectDictDataList(sysDictData);
//        ExcelUtil.exportExcel(list, "字典数据", SysDictData.class, response);
    }
}
